package com.ms.p2p.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.bean.Message;
import com.ms.common.utils.AppContext;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.entity.account.Account;
import com.ms.p2p.entity.account.YuEbao;
import com.ms.p2p.entity.account.YuEbaoLog;
import com.ms.p2p.entity.member.Member;
import com.ms.p2p.provider.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@Controller
@Transactional
@RequestMapping({ "/member/current" })
public class YuEbaoController extends BaseController {

	@Reference
	private YeahpaysProvider yeahpayProvider;

	@Reference
	private YeahpayLogProvider yeahpayLogProvider;

	@Reference
	private MemberAccountProvider memberAccountProvider;

	@Reference
	private PasswordProvider passwordProvider;

	@Reference
	private SettingProvider settingProvider;
	@Reference
	private YeahpaysProvider yeahpaysProvider;
	@RequestMapping({ "/open" })
	@ResponseBody
	public Message open(){
		Member user = getCurrentUser();
		YuEbao yuEbao = yeahpaysProvider.findByUserId(user.getId());
		if (yuEbao == null) {
			YuEbao yuEbaoImpl = new YuEbao();
			yuEbaoImpl.setUser_id(user.getId());
			yeahpaysProvider.insert(yuEbaoImpl);
		}
		return  Message.success("开通成功");
	}
	/**
	 * 活期宝
	 */
	@RequestMapping({ "/index" })
	public String toMyYuEbao(String currentPage, Model model) {
		Member user = getCurrentUser();
		YuEbao yuEbao = yeahpayProvider.findByUserId(user.getId());
		if (yuEbao == null) {
			model.addAttribute("status", 0);
		}else{
			model.addAttribute("status", 1);
		}
		PageBean page = new PageBean();
		if (StringUtils.isNotBlank(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		// 活期宝收益率（小数）
		String yield = settingProvider.findValueByCode("yuebao_yield");
		model.addAttribute("yield", Double.valueOf(yield));
		model.addAttribute("yuEbao", yuEbao);
		List<YuEbaoLog> yuEbaoLogs = yeahpayLogProvider.findPager(user.getId(), page);
		page.setTotalCount(yeahpayLogProvider.findCount(user.getId()));
		model.addAttribute("yuEbaoLogs", yuEbaoLogs);
		model.addAttribute("page", page);

		return "customer/member/ucenter/yuebao/index";
	}

	/**
	 * 到转入或转出活期宝页面
	 */
	@RequestMapping("/to/{turnType}")
	public String toIntoOrOutYuEbao(@PathVariable String turnType, Model model) {
		Member user =getCurrentUser();
		YuEbao yuEbao = yeahpayProvider.findByUserId(user.getId());
		if (yuEbao == null) {
			model.addAttribute("status", 0);
		}else{
			model.addAttribute("status", 1);
		}
		Account account = memberAccountProvider.findByUserId(user.getId());
		//YuEbao yuEbao = yeahpayProvider.findByUserId(user.getId());
		model.addAttribute("account", account);
		model.addAttribute("yuEbao", yuEbao);
		model.addAttribute("turnType", turnType);

		return "customer/member/ucenter/yuebao/input";
	}

	/**
	 * 转入转出到活期宝页面
	 */
	@RequestMapping("/inout/{turnType}")
	@ResponseBody
	public Message intoOrOutYuEbao(@PathVariable String turnType, String captcha, String money) {
		Member user =getCurrentUser();
		//验证码校验

		if (StringUtils.isBlank(money)) {
			return Message.error("转入金额不能为空！") ;
		}

		if (BigDecimal.valueOf(Double.valueOf(money)).compareTo(BigDecimal.valueOf(0L)) == -1) {
			return Message.error("转入金额不能为负！") ;
		}
		switch (turnType){
			case "into":
			{
				Double totalmoney = memberAccountProvider.sumYuebao().doubleValue();
				if(Double.valueOf(money)+totalmoney>1000000D){
					return  Message.error("该产品额度已满");
				}

				Account account = memberAccountProvider.findByUserId(user.getId());
				if (BigDecimal.valueOf(Double.valueOf(money)).compareTo(account.getBalance()) > 0) {
					return Message.error( "转入金额大于账户最大可用余额，请重新操作!");
				}
				yeahpayProvider.into(BigDecimal.valueOf(Double.valueOf(money)), user.getId(), "￥" + money + "转入活期宝。", AppContext.getRemoteIp());
				return Message.success("转入成功");
			}
			case "out":
			{
				YuEbao yuEbao = yeahpayProvider.findByUserId(user.getId());
				if (new BigDecimal(money).compareTo(yuEbao.getTotal()) > 0) {
					return Message.error("转出金额大于活期宝总额，请重新操作!");
				}
				yeahpayProvider.out(new BigDecimal(money), user.getId(), "￥" + money + "转出活期宝。", AppContext.getRemoteIp());
				return Message.success("转出成功");
			}

		}

		return errorMMessage;
	}
}
